//
// Created by lenovo on 2024/1/11.
//

#include "Slicer.h"
#include <set>

std::set<char> Num = Character::Nums;
std::set<std::string> Opt = Character::Operators;
#include <iostream>
using namespace std ;


void Slicer::rowSplit(){
    auto it = this->material.begin();
    std::string cur;

    bool is_num = true ;

    while (it != this->material.end()){
        //括号识别
        if (*it == '(' or (*it) == ')'){
            is_num = false ;
            if (not cur.empty())
                this->words.push_back(cur);
            cur.clear();

            this->words.push_back(cur + *it);
        }

        //空格识别
        else if (*it == ' '){
            is_num = false ;
            if (not cur.empty())
                this->words.push_back(cur);
            cur.clear();
        }

        //数字识别
        else if (Num.find(*it) != Num.end()){
            if (not is_num){
                if (not cur.empty())
                    this->words.push_back(cur);
                cur.clear();
            }
            is_num = true ;
            cur += *it ;
        }

        //其他字符连接
        else{
            if (is_num){
                if (not cur.empty())
                    this->words.push_back(cur);
                cur.clear();
            }
            is_num = false ;
            cur += *it ;
        }
        it ++ ;
    }
    if (!cur.empty()){
        this->words.push_back(cur);
    }

}

std::vector<std::string> Slicer::result() {
    if (not _done){
        this->parse();
    }
//    cout << "OK" << endl ;

    return this->words;
}

void Slicer::funcSplit() {
    int i = 0 ;
    while (i < this->words.size()){
        //从长度的角度观察是否有复合的可能
        if (this->words[i].size() == 1){
            i ++ ;
            continue;
        }

        //else
        if (this->words[i].find("**") == 0){
            if (this->words[i].size() == 2){
                i ++ ;
                continue;
            }
            std::string func ;
            for (int j = 2 ; j < this->words[i].size() ; j ++){
                func += this->words[i][j];
            }

            this->words.erase(this->words.begin() + i);
            this->words.insert(this->words.begin() + i,"**");
            this->words.insert(this->words.begin() + i + 1 ,func);
            i += 2 ;
            continue;
        }

        char word = this->words[i].front() ;
        std::string front;
        front += word ;

        if (Character::Operators.find(front) != Character::Operators.end()){
//            cout << "PreFix Opt" << endl ;
            if (this->words[i].size() == 1){
                i ++ ;
                continue;
            }

            string func ;
            for (int j = 1 ; j < this->words[i].size() ; j ++)
                func += this->words[i][j];
            this->words.erase( this->words.begin() + i );
            this->words.insert(this->words.begin() + i , front);
            this->words.insert( this->words.begin() + i + 1 , func);
            i += 2 ;
            continue ;

        }else{
//            cout << "Pure Function" << endl ;
            i ++ ;
            continue ;
        }

    }
}

void Slicer::parse() {
    this->rowSplit() ;//初步切分
    this->dotSplit() ;//将粘黏的逗号分开
    this->funcSplit();//将耦合的运算符与Func分开
}

void Slicer::dotSplit() {
    int i = 0 ;
    while(i < this->words.size()){
        std::string word = this->words[i];
        auto front = word.front() ;
        if (front == ',' and word.size() > 1){
            //识别到逗号
            word.erase(word.begin());
            this->words.erase(this->words.begin() + i);
            this->words.insert(
                    this->words.begin() + i ,
                    ","
                    );
            this->words.insert(
                    this->words.begin() + i + 1 ,
                    word
                    );
            i ++ ;
        }
        i ++ ;
    }
}
